﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using EGov;
using System.Runtime.Serialization.Json;
using System.Text.RegularExpressions;

namespace EGov
{
    public partial class dangki : PhoneApplicationPage
    {
        public dangki()
        {
            InitializeComponent();
            string url = "http://egov047.juddy.vn/api/user";

            WebClient req = new WebClient();
            req.UploadStringAsync(new Uri(url), "POST");
            
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            string username = txtUsername.Text;
            string pass = pwdPass.Password;
            string email = txtEmail.Text;
            string phone = txtPhone.Text;
            
            DateTime today = DateTime.Now;
            DateTime birthday = dpkBirthday.Value.Value;
            int result = DateTime.Compare(today, birthday);
            
            if (username != "" && pass != "" && email != "" && phone != "" && txtName.Text != "")
            {
                if (pass.Length >= 6)
                {
                    if (isPhone(phone))
                    {
                        if (isEmail(email))
                        {
                            if (result==1)
                            {
                                string link = "http://egov047.juddy.vn/api/Accounts/Registration?userName=" + username.Trim() + "&pass=" + pass.Trim() + "&phone=" + phone.Trim() + "&email=" + email.Trim() + "&avatar=a&bio=abc&groupId=2";
                                MessageBox.Show(link);
                                WebClient client = new WebClient();
                                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
                                client.OpenReadAsync(new Uri(link), UriKind.Absolute);
                            }
                            else
                            {
                                MessageBox.Show("Ngay sinh khong duoc lon hon ngay hien tai");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Dia chi email khong hop le!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("So dien thoai ban nhap khong hop le!");
                    }
                }
                else
                {
                    MessageBox.Show("Mat khau toi thieu la 6 ki tu");
                }
            }
            else
            {
                MessageBox.Show("Vui long nhap day du thong tin de tao tai khoan!");
            }
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                var ser = new DataContractJsonSerializer(typeof(bool), null);
                bool result = (bool)ser.ReadObject(e.Result);

                if (result)
                {
                    MessageBox.Show("Chuc mung ban da dang ky thanh cong!");
                    NavigationService.Navigate(new Uri("/dangnhap.xaml", UriKind.Relative));
                }
                else
                {
                    MessageBox.Show("Thong tin nhap chua hop le, vui long kiem tra lai hoac tai khoan da duoc su dung!");
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
                MessageBox.Show("Thong tin nhap chua hop le, vui long kiem tra lai hoac tai khoan da duoc su dung!");
            }
        }

        bool isEmail(string mail)
        {
            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(mail);
            return match.Success;
        }

        bool isPhone(string phone)
        {
            Regex regex = new Regex(@"^\d{10}$");
            Match match = regex.Match(phone);
            return match.Success;
        }
    }
}